﻿--[[
   @brief    model for group message
   @author   vincentlao
   @version  1.0
   @date     2015-06-26
--]]

group_message_model =
{
    ierd_tgp.tpf_ui.Require("social\\group_list\\model.lua");
    ierd_tgp.tpf_ui.Require("social\\talk_dialog\\control.lua");

    group_message_= {};
    group_new_member_message_ = {};
    MAX_GROUP_MESSAGE = 500;

    message_sort_func = function(a, b)
        local a_msg_time = a.time;
        local b_msg_time = b.time; 
        if a_msg_time == nil then
            return false;
        end

        if b_msg_time == nil then
            return true;
        end

        return (a_msg_time > b_msg_time);
    end;

    init = function()
        ierd_tgp.tpf_ui.Log("[group_message_model]init");
        group_message_model.group_message_= {};
    end;

    uninit = function()
        ierd_tgp.tpf_ui.Log("[group_message_model]uninit");
    end;
    
    add_message = function(message)
      if group_message_model.group_message_[message.group_id] == nil then
          group_message_model.group_message_[message.group_id] = {};
      end

      if #group_message_model.group_message_[message.group_id] > group_message_model.MAX_GROUP_MESSAGE then
          table.remove(group_message_model.group_message_[message.group_id], #group_message_model.group_message_[message.group_id]);
      end

      table.insert(group_message_model.group_message_[message.group_id], 1, message);
    end;

    --[[add_msg_order = function(message)
        if group_message_model.group_message_[message.group_id] == nil then
            group_message_model.group_message_[message.group_id] = {};
        end

        if #group_message_model.group_message_[message.group_id] > group_message_model.MAX_GROUP_MESSAGE then
           table.remove(group_message_model.group_message_[message.group_id], #group_message_model.group_message_[message.group_id]);
        end

        local messages = group_message_model.group_message_[message.group_id]

        for messages_i = 1, #messages do
            if message.time >= messages[messages_i].time then
                table.insert(messages, messages_i, message);
                break;
            end
        end
        table.insert(messages, message);    
    end;]]--

    get_group_messages = function(group_id)
        if group_message_model.group_message_[group_id] == nil then
            group_message_model.group_message_[group_id] = {};
        end
        
        return group_message_model.group_message_[group_id];
    end;

    get_group_last_message = function(group_id)
        local group_messages = group_message_model.group_message_[group_id];
        if group_messages == nil or #group_messages == 0 then
            return nil;
        end

        return group_messages[1];  
    end;

    clear_all_group_messages = function()
        group_message_model.group_message_ = {};
    end;

    clear_group_messages = function(group_id)
        if group_message_model.group_message_[group_id] == nil then
            return;
        end

        if #group_message_model.group_message_[group_id] == 0 then
            return;
        end

        group_message_model.group_message_[group_id] = {};
    end;

    get_message_count = function(group_id)
        if group_message_model.group_message_[group_id] == nill then
          return 0;
        end

        local count = 0;
        for i = 1, #group_message_model.group_message_[group_id] do
            if group_message_model.group_message_[group_id][i].action_type ~= talk_dialog_control.NEW_GROUP_NEW_MEMBER_MSG_TYPE then
                count = count + 1;
            end
        end
        return count;
    end;

    add_new_member_message = function(message)
      if group_message_model.group_new_member_message_[message.group_id] == nil then
          group_message_model.group_new_member_message_[message.group_id] = {};
      end

      table.insert(group_message_model.group_new_member_message_[message.group_id], 1, message);
    end;

    get_group_new_member_messages = function(group_id)
        if group_message_model.group_new_member_message_[group_id] == nil then
            group_message_model.group_new_member_message_[group_id] = {};
        end
        
        return group_message_model.group_new_member_message_[group_id];
    end;   

    clear_all_group_new_member_messages = function()
        group_message_model.group_new_member_message_ = {};
    end;

    clear_group_new_member_messages = function(group_id)
        if group_message_model.group_new_member_message_[group_id] == nil then
            return;
        end

        if #group_message_model.group_new_member_message_[group_id] == 0 then
            return;
        end

        group_message_model.group_new_member_message_[group_id] = {};
    end;
};

group_message_model.init();
